﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Pele
{
    public class PCorPele
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);

        public bool Salvar(string nmCorPele)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbCorPele = new tb_cor_pele();
                tbCorPele.nm_cor_pele = nmCorPele;
                _db.AddTotb_cor_pele(tbCorPele);
                _db.SaveChanges();

                return true;

            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public DataSet pegarCoresPele(string strNmCorPele)
        {
            var ds = new DataSet();

            string query = "SELECT ";
            query += " id_cor_pele";
            query += ", nm_cor_pele";
            query += " FROM tb_cor_pele";

            query += " WHERE 1 = 1";


            if (!string.IsNullOrEmpty(strNmCorPele))
            {
                query += " AND nm_cor_cabelo LIKE '%" + strNmCorPele + "%'";
            }

            var cmd = new SqlCommand(query, _conexao);

            cmd.Connection = _conexao;

            if (_conexao.State == ConnectionState.Closed)
            {
                _conexao.Open();
            }

            var dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(ds);

            _conexao.Close();

            return ds;
        }

        public bool Remover(int idCorPele)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbCorPele = _db.tb_cor_pele.Where(q => q.id_cor_pele == idCorPele).FirstOrDefault();
                if (obTbCorPele == null)
                {
                    return false;
                }
                else
                {
                    _db.tb_cor_pele.DeleteObject(obTbCorPele);
                    _db.SaveChanges();
                    return true;
                }

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public bool Alterar(int idCorPele, tb_cor_pele obTbCorPeleNovo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbCorPeleVelho =
                    _db.tb_cor_pele.Where(q => q.id_cor_pele == idCorPele).FirstOrDefault();

                obTbCorPeleVelho.nm_cor_pele = obTbCorPeleNovo.nm_cor_pele;

                _db.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public tb_cor_pele pegarCorCabeloPorId(int idCorPele)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_cor_pele.Where(q => q.id_cor_pele == idCorPele).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }
    }
}
